﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthPostAction("ETL", "UpdateDataTableData", "1.0")]
    public class UpdateDataTableDataAction : BaseAction<UpdateDataTableDataParams, UpdateDataTableDataResult>
    {
        protected override ActionResult<UpdateDataTableDataResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            UpdateDataTableDataParams opParams)
        {
            string[] pkColumnArr = opParams.PKColumns.Split(new char[] { ',' });
            string[] pkValueArr = opParams.PKValues.Split(new char[] { ',' });

            SER.DtTableService service = new SER.DtTableService();
            ServiceResult result = service.UpdateData(context.UserId, opParams.TableId, pkColumnArr, pkValueArr, opParams.ColumnName, opParams.Value);
            if(result.IsSuccess)
            {
                return new ActionResult<UpdateDataTableDataResult>(new UpdateDataTableDataResult());
            }
            else
            {
                return new ActionResult<UpdateDataTableDataResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class UpdateDataTableDataParams
    {
        [Parameter]
        public string ColumnName { get; set; }

        /// <summary>
        /// 数据主键列信息（pk1,pk2,pk3）,顺序要和值的顺序一样
        /// </summary>
        public string PKColumns { get; set; }

        /// <summary>
        /// 数据主键的值信息（pkvalue1,pkvalue2,pkvalue3),顺序要和名的顺序一样
        /// </summary>
        public string PKValues { get; set; }

        [Parameter]
        public Guid TableId { get; set; }

        [Parameter]
        public string Value { get; set; }
    }

    public class UpdateDataTableDataResult
    {
    }
}
